package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.BannerAdapter;
import com.github.postsanf.yinian.bean.YNBanner;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.service.AlbumService;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.SpUtils;
import com.github.postsanf.yinian.view.CirclePageIndicator;
import com.github.postsanf.yinian.view.LoopViewPager;

import java.util.ArrayList;
import java.util.List;

public class ActivityActivity extends BaseActivity implements View.OnClickListener {
    private LoopViewPager adViewPager;
    private CirclePageIndicator mPagerIndicator;             // 指示器
    private ImageView iv_back;
    private RelativeLayout rl_space;
    private RelativeLayout rl_join;
    private RelativeLayout rl_link;
    private RelativeLayout rl_memory;
    private RelativeLayout rl_save;
    private RelativeLayout rl_share;
    private RelativeLayout rl_lock;
    private RelativeLayout rl_lose;
    private RelativeLayout rl_change;
    private List<YNBanner> adList = new ArrayList<>();
    private BannerAdapter mBannerAdapter;
    private ImageView iv_activity_new;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity);
        registerBroadcastReceiver();
        initAdData();
        initView();
    }

    private void initAdData() {
        adViewPager = (LoopViewPager) findViewById(R.id.vp_home_banner);
        mPagerIndicator = (CirclePageIndicator) findViewById(R.id.banner_indicator);
        int width = DisplayUtils.getScreenWidthPixels(ActivityActivity.this) - UIHelper.dip2px(ActivityActivity.this, 20);
        adViewPager.getLayoutParams().height = 8 * width / 10;
        adViewPager.requestLayout();
    }

    private void initView() {
        iv_back = ((ImageView) findViewById(R.id.iv_back));
        rl_space = ((RelativeLayout) findViewById(R.id.rl_space));
        rl_join = ((RelativeLayout) findViewById(R.id.rl_join));
        rl_link = ((RelativeLayout) findViewById(R.id.rl_link));
        rl_memory = ((RelativeLayout) findViewById(R.id.rl_memory));
        rl_save = ((RelativeLayout) findViewById(R.id.rl_save));
        rl_share = ((RelativeLayout) findViewById(R.id.rl_share));
        rl_lock = ((RelativeLayout) findViewById(R.id.rl_lock));
        rl_lose = ((RelativeLayout) findViewById(R.id.rl_lose));
        rl_change = ((RelativeLayout) findViewById(R.id.rl_change));
        iv_activity_new = (ImageView) findViewById(R.id.iv_activity_new);
        boolean aNew = SpUtils.getBoolean(ActivityActivity.this, "new");
        if (!aNew) {
            iv_activity_new.setVisibility(View.VISIBLE);
        } else {
            iv_activity_new.setVisibility(View.GONE);
        }
        iv_back.setOnClickListener(this);
        rl_space.setOnClickListener(this);
        rl_join.setOnClickListener(this);
        rl_link.setOnClickListener(this);
        rl_memory.setOnClickListener(this);
        rl_save.setOnClickListener(this);
        rl_share.setOnClickListener(this);
        rl_lock.setOnClickListener(this);
        rl_lose.setOnClickListener(this);
        rl_change.setOnClickListener(this);
    }

    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actitityBannerUpdate);
        myIntentFilter.addAction(ACTIONs.network);
        registerReceiver(mCameraBroadcastReceiver, myIntentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mCameraBroadcastReceiver);
    }

    // 分析并执行动作
    BroadcastReceiver mCameraBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actitityBannerUpdate:
                    List<YNBanner> list = (List<YNBanner>) intent.getExtras().getSerializable(ACTIONs.aAddData);
                    adList.clear();
                    adList.addAll(list);
                    if (adList.size() > 0) {
                        mBannerAdapter = new BannerAdapter(ActivityActivity.this, adList);
                        mBannerAdapter.setActivity(true);
                        adViewPager.setAdapter(mBannerAdapter);
                        mPagerIndicator.setViewPager(adViewPager);
                        mBannerAdapter.setOnBinnerClickListener(new BannerAdapter.OnBinnerClickListener() {
                            @Override
                            public void onBinnerClickListener() {
                                iv_activity_new.setVisibility(View.GONE);
                                SpUtils.put(ActivityActivity.this, "new", true);
                            }
                        });
                    }
                    break;
                case ACTIONs.network:
                    noticeBannerInit();
                    break;
            }
        }
    };

    private void noticeBannerInit() {
        Intent bIntent = new Intent(ActivityActivity.this, AlbumService.class);
        bIntent.setAction(ACTIONs.activityBinner);
        startService(bIntent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_space:
                Intent intent = new Intent(ActivityActivity.this, CourseActivity.class);
                intent.putExtra("title", "基本功能");
                intent.putExtra("question", "圈子可以用来做什么？");
                intent.putExtra("answer", "圈子是你和你的好友家人共同记录分享的空间，内容仅对当前圈子内的成员可见。");
                startActivity(intent);
                break;
            case R.id.rl_join:
//                Intent intent1 = new Intent(ActivityActivity.this, CourseActivity.class);
//                intent1.putExtra("title", "基本功能");
//                intent1.putExtra("question", "怎么邀请好友加入空间？");
//                intent1.putExtra("answer", "链“查看教程”。");
//                startActivity(intent1);
                intent2Activity(AwardRegisterActivity.class);
                break;
            case R.id.rl_link:
                Intent intent2 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent2.putExtra("title", "基本功能");
                intent2.putExtra("question", "忆年还能通过微信端和PC端使用？");
                intent2.putExtra("answer", "微信端：1.关注微信公众号（yinian0230），点击“忆年圈子”，可以创建空间或查看圈子，在圈子内能查看照片墙，还能一键导入手机照片！点击“开始制作”还能制作记录卡片和语音图集！\n" +
                        "              2.打开照片墙的分享链接（可以是你分享给别人的，也可以是别人分享给你的），可以查看圈子内的所有照片，还能快捷导入手机照片！\n" +
                        "      PC端：在PC端网页输入官网网址（www.zhuiyinanian.cn），登录后就可以查看不同圈子内的照片墙，还能快捷上传电脑照片！\n");
                startActivity(intent2);
                break;
            case R.id.rl_memory:
                Intent intent3 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent3.putExtra("title", "更多");
                intent3.putExtra("question", "记录共享的内容会存在哪里？");
                intent3.putExtra("answer", "在圈子内通过一键上传，语音图集和记录卡和一日一记共享的内容，都可以在时光轴上看到哦，同时所上传的图片还会在照片墙中显示！");
                startActivity(intent3);
                break;
            case R.id.rl_save:
                Intent intent4 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent4.putExtra("title", "更多");
                intent4.putExtra("question", "如果想自己存一些照片可以吗？");
                intent4.putExtra("answer", "打开“我的”，点击“私人相册”，就可以快捷导入自己想要保存的照片，永不丢失，还能节省手机内存哦！");
                startActivity(intent4);
                break;
            case R.id.rl_share:
                Intent intent5 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent5.putExtra("title", "更多");
                intent5.putExtra("question", "分享的时候选不同的方式有什么区别？");
                intent5.putExtra("answer", "我们共有三种不同的分享方式。\n" +
                        "      方式一：图片分享。选择您想要分享的图片，我们将以拼图的方式发送给您分享的用户。\n" +
                        "      方式二：链接分享。选择您想要分享的内容（记录卡片、语音图集或一日一记），我们将以网页链接的方式发送给你分享的用户。\n" +
                        "      方式三：二维码分享。选择您要分享的内容（记录卡片、语音图集或一日一记），我们将会为您分享的内容生成独立的二维码，发送给您分享的用户。只要在手机上识别二维码，就能查看到您分享的内容了。\n");
                startActivity(intent5);
                break;
            case R.id.rl_lock:
                Intent intent6 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent6.putExtra("title", "隐私与安全");
                intent6.putExtra("question", "如何设置屏幕密码锁？");
                intent6.putExtra("answer", "在“我的”中，点击屏幕右上角的设置按钮，打开密码锁，即可社会屏幕密码锁。");
                startActivity(intent6);
                break;
            case R.id.rl_lose:
                Intent intent7 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent7.putExtra("title", "隐私与安全");
                intent7.putExtra("question", "如果把照片放这里会不会丢失？");
                intent7.putExtra("answer", "所有上传的照片都存储在七牛云加密服务器中，并对数据做了加密处理，同时会定期将新增的照片进行备份，避免丢失。（七牛云是国内顶级的企业级云服务商，提供专业、优质的图片存储服务）");
                startActivity(intent7);
                break;
            case R.id.rl_change:
                Intent intent8 = new Intent(ActivityActivity.this, CourseActivity.class);
                intent8.putExtra("title", "隐私与安全");
                intent8.putExtra("question", "如果更换设备，共享的内容会不会丢失？");
                intent8.putExtra("answer", "忆年支持云端自动备份，只要使用相同的账号登录，共享过的内容都可以找到！");
                startActivity(intent8);
                break;
        }
    }
}
